package com.wish.g04.house.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wish.g04.base.entity.*;
import com.wish.g04.base.service.*;
import com.wish.g04.common.core.constant.HttpStatus;
import com.wish.g04.common.core.utils.StringUtils;
import com.wish.g04.common.core.utils.bean.BeanUtils;
import com.wish.g04.common.core.web.domain.AjaxResult;
import com.wish.g04.common.core.web.page.TableDataInfo;
import com.wish.g04.house.dto.PremisesInfoDto;
import com.wish.g04.house.service.TbPremisesInfoService;
import com.wish.g04.house.vo.PremisesInfoVo;
import com.wish.g04.system.api.RemoteDictService;
import com.wish.g04.system.api.domain.DictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 楼盘接口
 */

@Service
public class TbPremisesInfoServiceImpl implements TbPremisesInfoService {

//    redis存储社区类型的key
    private final String communityType="house_community_type";

    @Autowired
    private IPremisesInfoService iPremisesInfoService;

//    地区sevice
    @Autowired
    private IRegionService iRegionService;

//    feign 字典service
    @Resource
    private RemoteDictService remoteDictService;

    //    添加相册内容
    @Autowired
    private IAlbumService iAlbumService;

//    楼盘特色
    @Autowired
    private IConfigPremisesPropertyService iConfigPremisesPropertyService;
//    楼盘-特色关系
    @Autowired
    private IRelationPremisesPropertyService iRelationPremisesPropertyService;
//    楼盘配套表
    @Autowired
    private IPremisesFacilityService iPremisesFacilityService;

    @Override
    public TableDataInfo getList(PremisesInfoDto tbPremisesInfoDto) {
        QueryWrapper<PremisesInfo> wrapper = new QueryWrapper<>();
//        楼盘名称或楼盘地址
        if(StringUtils.isNotEmpty(tbPremisesInfoDto.getName())){
            wrapper.or(wrapper1 -> wrapper1.like("name",tbPremisesInfoDto.getName()).or().like("address",tbPremisesInfoDto.getName()));
        }
//        物业类型
        if(StringUtils.isNotEmpty(tbPremisesInfoDto.getPremisesType())){
            wrapper.eq("premises_type",tbPremisesInfoDto.getPremisesType());
        }
//        省
        if(tbPremisesInfoDto.getProvince()!=null){
            wrapper.eq("province",tbPremisesInfoDto.getProvince());
        }
        //        市
        if(tbPremisesInfoDto.getCity()!=null){
            wrapper.eq("city",tbPremisesInfoDto.getCity());
        }
        //        区
        if(tbPremisesInfoDto.getCounty()!=null){
            wrapper.eq("county",tbPremisesInfoDto.getCounty());
        }
        wrapper.orderByDesc("id");

        List<PremisesInfo> list = iPremisesInfoService.list(wrapper);
        TableDataInfo tableDataInfo = getTableDataInfo(list);

//        存储vo对象的集合
        ArrayList<PremisesInfoVo> premisesInfoVos = new ArrayList<>();
        //        查询社区类型
        List<DictData> dictDataByType = getDictDataByType(communityType);

//        转vo对象
        for (PremisesInfo premisesInfo : list) {

            PremisesInfoVo premisesInfoVo = new PremisesInfoVo();
            BeanUtils.copyProperties(premisesInfo,premisesInfoVo);

            for (DictData dictData : dictDataByType) {
//                判断社区类型 键值相等                             实际存储的社区类型键值
                if(dictData.getDictValue().equals(premisesInfo.getCommunityType())){
                    premisesInfoVo.setCommunityTypeLable(dictData.getDictLabel());
                }
            }
            premisesInfoVos.add(premisesInfoVo);
        }
        tableDataInfo.setRows(premisesInfoVos);
        return tableDataInfo;
    }

    @Override
    public List<Region> getRegion(Integer parentId) {
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",parentId);
        List<Region> list = iRegionService.list(wrapper);
        return list;
    }

    @Override
    public List<DictData> getDictDataByType(String dictType) {
        List<DictData> dictDataList =  remoteDictService.getDictDataByType(dictType);
        return dictDataList;
    }

    @Override
    public TableDataInfo getTableDataInfo(List<PremisesInfo> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    @Override
    public void save(PremisesInfoDto premisesInfoDto) {
        PremisesInfo premisesInfo = new PremisesInfo();
        BeanUtils.copyProperties(premisesInfoDto,premisesInfo);
//        添加楼盘基本信息
        iPremisesInfoService.saveOrUpdate(premisesInfo);
//        添加楼盘与楼盘特色关系
        QueryWrapper<RelationPremisesProperty> wrapper = new QueryWrapper<>();
        wrapper.eq("premises_id",premisesInfo.getId());
        iRelationPremisesPropertyService.remove(wrapper);//删除关系表
        List<Integer> propertyIds = premisesInfoDto.getPropertyIds();
        System.out.println(propertyIds);
        for (Integer propertyId : propertyIds) {
            iRelationPremisesPropertyService.saveOrUpdate(new RelationPremisesProperty().
                    setPremisesId(premisesInfo.getId()).
                    setPropertyId(propertyId));
        }

//       添加周边配套
        QueryWrapper<PremisesFacility> premisesFacilityWrapper = new QueryWrapper<>();
        premisesFacilityWrapper.eq("premises_id",premisesInfo.getId());
        iPremisesFacilityService.remove(premisesFacilityWrapper);

        List<PremisesFacility> peripheralFacilities = premisesInfoDto.getPeripheralFacilities();
        for (PremisesFacility premisesFacility : peripheralFacilities) {
            iPremisesFacilityService.saveOrUpdate(new PremisesFacility().setPremisesId(premisesInfo.getId())
                    .setContent(premisesFacility.getContent())//内容
                    .setTitle(premisesFacility.getTitle())//标题
                    .setType(0));//配套
        }

        //       添加交通
        List<PremisesFacility> peripheralFacilities2 = premisesInfoDto.getPeripheralFacilities();
        for (PremisesFacility premisesFacility : peripheralFacilities2) {
            iPremisesFacilityService.saveOrUpdate(new PremisesFacility().setPremisesId(premisesInfo.getId())
                    .setContent(premisesFacility.getContent())//内容
                    .setTitle(premisesFacility.getTitle())//标题
                    .setType(1));//交通
        }
//        添加相册
        iAlbumService.remove(new QueryWrapper<Album>().eq("premises_id",premisesInfo.getId()));
        if(StringUtils.isNotEmpty(premisesInfoDto.getPic1())){
            iAlbumService.saveOrUpdate(new Album().setAlbumType(1)
                    .setPremisesId(premisesInfo.getId())
                    .setUrl(premisesInfoDto.getPic1())
                    .setName("file"));
        }
        if(StringUtils.isNotEmpty(premisesInfoDto.getPic2())){
            iAlbumService.saveOrUpdate(new Album().setAlbumType(2)
                    .setPremisesId(premisesInfo.getId())
                    .setUrl(premisesInfoDto.getPic2())
                    .setName("file"));
        }
        if(StringUtils.isNotEmpty(premisesInfoDto.getPic3())){
            iAlbumService.saveOrUpdate(new Album().setAlbumType(3)
                    .setPremisesId(premisesInfo.getId())
                    .setUrl(premisesInfoDto.getPic3())
                    .setName("file"));
        }
        if(StringUtils.isNotEmpty(premisesInfoDto.getPic4())){
            iAlbumService.saveOrUpdate(new Album().setAlbumType(4)
                    .setPremisesId(premisesInfo.getId())
                    .setUrl(premisesInfoDto.getPic4())
                    .setName("file"));
        }
        if(StringUtils.isNotEmpty(premisesInfoDto.getPic5())){
            iAlbumService.saveOrUpdate(new Album().setAlbumType(5)
                    .setPremisesId(premisesInfo.getId())
                    .setUrl(premisesInfoDto.getPic5())
                    .setName("file"));
        }
    }

    @Override
    public List<ConfigPremisesProperty> getPremisesProperty() {
        List<ConfigPremisesProperty> list = iConfigPremisesPropertyService.list();
        return list;
    }

    @Override
    public AjaxResult getInfo(Long id) {
        HashMap<String, Object> map = new HashMap<>();

        PremisesInfo byIdPremises = iPremisesInfoService.getById(id);
        //商圈
        Integer[] bcIds = new Integer[0];
        String commercialArea = byIdPremises.getCommercialArea();
        if(StringUtils.isNotEmpty(commercialArea)){
            String[] split = commercialArea.split(",");
            bcIds = Convert.toIntArray(split);
        }


        //地铁
        String subway = byIdPremises.getSubway();
        Integer[] subIds = new Integer[0];
        if(StringUtils.isNotEmpty(subway)){
            String[] split1 = subway.split(",");
            subIds = Convert.toIntArray(split1);
        }


//        楼盘特色
        QueryWrapper<RelationPremisesProperty> wrapper = new QueryWrapper<>();
        wrapper.eq("premises_id",id);
        //特色
        List<RelationPremisesProperty> list = iRelationPremisesPropertyService.list(wrapper);
        ArrayList<Integer> ts = new ArrayList<>();
        for (RelationPremisesProperty property : list) {
            ts.add(property.getPropertyId());
        }

//        楼盘配套
        QueryWrapper<PremisesFacility> premisesFacilityQueryWrapper = new QueryWrapper<>();
        premisesFacilityQueryWrapper.eq("premises_id",id)
                .eq("type",0);
        List<PremisesFacility> peripheralFacilities = iPremisesFacilityService.list(premisesFacilityQueryWrapper);

//        交通情况
        QueryWrapper<PremisesFacility> premisesFacilityQueryWrapper2 = new QueryWrapper<>();
        premisesFacilityQueryWrapper2.eq("premises_id",id)
                .eq("type",1);
        List<PremisesFacility> trafficCondition = iPremisesFacilityService.list(premisesFacilityQueryWrapper2);

//        图片回显
        QueryWrapper<Album> picWrapper = new QueryWrapper<>();
        picWrapper.eq("premises_id",id);
        List<Album> picList = iAlbumService.list(picWrapper);
        ArrayList<Album> pic1 = new ArrayList<>();
        ArrayList<Album> pic2 = new ArrayList<>();
        ArrayList<Album> pic3 = new ArrayList<>();
        ArrayList<Album> pic4 = new ArrayList<>();
        ArrayList<Album> pic5 = new ArrayList<>();
        HashMap<String, Object> pic = new HashMap<>();

        for (Album album : picList) {
            if(album.getAlbumType()==1){
//                pic1.add(album);
                pic.put("pic1",album.getUrl());
            }
            if(album.getAlbumType()==2){
//                pic2.add(album);
                pic.put("pic2",album.getUrl());
            }
            if(album.getAlbumType()==3){
//                pic3.add(album);
                pic.put("pic3",album.getUrl());
            }
            if(album.getAlbumType()==4){
//                pic4.add(album);
                pic.put("pic4",album.getUrl());
            }
            if(album.getAlbumType()==5){
//                pic5.add(album);
                pic.put("pic5",album.getUrl());
            }
        }
//        HashMap<String, Object> pic = new HashMap<>();
//        pic.put("pic1",pic);
//        pic.put("pic2",pic);
//        pic.put("pic3",pic);
//        pic.put("pic4",pic);
//        pic.put("pic5",pic);


        map.put("byId",byIdPremises);
        map.put("bcIds",bcIds);
        map.put("subIds",subIds);
        map.put("ts",ts);
        map.put("peripheralFacilities",peripheralFacilities);
        map.put("trafficCondition",trafficCondition);
        map.put("pic",pic);
        return AjaxResult.success(map);
    }
}
